package py.com.alfa.escriba.gui.rest.services.resttemplate;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;

import py.com.alfa.escriba.gui.config.EscribaConfigApplicationContextProvider;
import py.com.alfa.escriba.gui.dto.UserResponseDTO;
import py.com.alfa.escriba.gui.rest.services.LoginService;
import py.com.alfa.escriba.gui.services.Services;
import py.com.sif.rest.client.spring.request.ClientRequestBuilder;

@SuppressWarnings({ "unchecked", "unused"})
public class LoginServiceClient implements LoginService {

	private Logger log = LoggerFactory.getLogger(getClass());
	
	public LoginServiceClient() {}

	@Override
	public UserResponseDTO validUser(String user, String clave) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addHeader("user", user);
		request.addHeader("pwd", clave);
		ResponseEntity<UserResponseDTO> response = (ResponseEntity<UserResponseDTO>) request.exchange(VALIDA_USER, HttpMethod.GET, UserResponseDTO.class);
		UserResponseDTO result = response.getBody();
		return result;
	}

}
